package org.example.dyscript.dynamicscript;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import lombok.SneakyThrows;

import java.util.Map;

public class QLExpressRule implements Rule {

  private static final ExpressRunner runner = new ExpressRunner();
  ;

  String executeScript = "ali_score > 100 "
      + "&& wx_score < 450 "
      + "&& !pd_living_province.contains(\"乌克兰\") "
      + "&& pd_census_province.contains(\"吉尔吉斯斯坦\")";

  String evaluateScript = "age > 18 && vip.contains(\"vip\")";

  public QLExpressRule() {
  }

  @SneakyThrows
  @Override
  public boolean execute(Map<String, Object> parameters) {
    DefaultContext<String, Object> context = new DefaultContext<>();
    context.putAll(parameters);
    return (boolean) runner.execute(executeScript, context, null, true, false);
  }

  @SneakyThrows
  @Override
  public boolean evaluate(Map<String, Object> parameters) {
    DefaultContext<String, Object> context = new DefaultContext<>();
    context.putAll(parameters);
    return (boolean) runner.execute(evaluateScript, context, null, true, false);
  }
}
